package rabbit.ack;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import rabbit.util.RabbitMqUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Work03
 * Package: rabbit.ack
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-25
 * Time: 17:13
 */

//消息在手动应答时候不丢失 放回队列中重新消费

public class Work03 {

    //消息队列
    public static final String TASK_QUEUE_NAME = "ack_queue";

    //接收消息
    public static void main(String[] args) throws IOException, TimeoutException {
        Channel chanel = RabbitMqUtils.getChanel();
        System.out.println("C1等待接收消息处理时间较短: ");

        //接收消息回调
        DeliverCallback deliverCallback = (consumerTag,message) ->{
            //收消息之前 睡觉
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("接收到的消息: " + new String(message.getBody(),"UTF-8"));

            //手动应答 使用信道应答
            //参数 消息的唯一表示 是否批量应答 false表示不批量应答
            chanel.basicAck(message.getEnvelope().getDeliveryTag(),false);

        };

        //设置不公平分发 默认是0 0就是公平分发
        chanel.basicQos(1);
        // 1 就是不公平分发

        //手动的接收消息
        chanel.basicConsume(TASK_QUEUE_NAME,false,deliverCallback,(consumerTag)->{
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        });

    }

}
